<?php
/**
 * 余额支付插件
 * Author: IT宇宙人
 * Date: 2015-09-09
 */

//namespace plugins\payment\alipay;

use Think\Model\RelationModel;
/**
 * 支付 逻辑定义
 * Class AlipayPayment
 * @package Home\Payment
 */

class balance extends RelationModel
{    
    public $tableName = 'plugin'; // 插件表            
    
    /**
     * 析构流函数
     */
    public function  __construct() {   
        parent::__construct();
    }
    /**
     * 生成支付代码
     * @param   array   $order      订单信息
     * @param   array   $config_value    支付方式信息
     */
    function get_code($order, $config_value)
    {
        $url = SITE_URL.U('Payment/returnUrl',array('pay_code'=>'balance','order_sn'=>$order['order_sn']));
        return "<script>location.href='".$url."';</script>";
    }
    
    /**
     * 页面跳转响应操作给支付接口方调用
     */
    function respond2()
    {
        // 余额支付逻辑
        $order_sn = I('order_sn');
        if (empty($order_sn)) {
            return array('status'=>0,'order_sn'=>$order_sn);
        }
        $order = M('order')->where("order_sn = {$order_sn}")->find();
        if($order['pay_status'] == 1){
            return array('status'=>1,'order_sn'=>$order_sn);
        }
        $user = session('user');
        if (!$user) {
            return array('status'=>0,'order_sn'=>$order_sn);
        }
        // 获取用户数据
        $user = M('users')->where("user_id = {$user['user_id']}")->find();
        // 判断余额是否足够
        if ($user['user_money'] < $order['order_amount']) {
            return array('status'=>0,'order_sn'=>$order_sn,'msg'=>'余额不足,请充值后支付!');
        }
        // 扣除余额
        accountLog($user['user_id'], ($order['order_amount'] * -1), 0,"余额支付",0,$order['order_sn'],$order['order_id']);
        // 更新订单状态为已支付
        update_pay_status($order_sn);
        return array('status'=>1,'order_sn'=>$order_sn);
    }
    
    
}